<template>
  <el-dialog
    :title="!dataForm.afterId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单号" prop="orderNum">
      <el-input v-model="dataForm.orderNum" placeholder="订单号"></el-input>
    </el-form-item>
    <el-form-item label="运费" prop="freight">
      <el-input v-model="dataForm.freight" placeholder="运费"></el-input>
    </el-form-item>
    <el-form-item label="退货留言" prop="saleLeave">
      <el-input v-model="dataForm.saleLeave" placeholder="退货留言"></el-input>
    </el-form-item>
    <el-form-item label="退货图片" prop="seleImg">
      <el-input v-model="dataForm.seleImg" placeholder="退货图片"></el-input>
    </el-form-item>
    <el-form-item label="退货时间" prop="seleDate">
      <el-input v-model="dataForm.seleDate" placeholder="退货时间"></el-input>
    </el-form-item>
    <el-form-item label="申请退货外键id" prop="forId">
      <el-input v-model="dataForm.forId" placeholder="申请退货外键id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          afterId: 0,
          orderNum: '',
          freight: '',
          saleLeave: '',
          seleImg: '',
          seleDate: '',
          forId: ''
        },
        dataRule: {
          orderNum: [
            { required: true, message: '订单号不能为空', trigger: 'blur' }
          ],
          freight: [
            { required: true, message: '运费不能为空', trigger: 'blur' }
          ],
          saleLeave: [
            { required: true, message: '退货留言不能为空', trigger: 'blur' }
          ],
          seleImg: [
            { required: true, message: '退货图片不能为空', trigger: 'blur' }
          ],
          seleDate: [
            { required: true, message: '退货时间不能为空', trigger: 'blur' }
          ],
          forId: [
            { required: true, message: '申请退货外键id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.afterId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.afterId) {
            this.$http({
              url: this.$http.adornUrl(`/patient/aftersale/info/${this.dataForm.afterId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderNum = data.aftersale.orderNum
                this.dataForm.freight = data.aftersale.freight
                this.dataForm.saleLeave = data.aftersale.saleLeave
                this.dataForm.seleImg = data.aftersale.seleImg
                this.dataForm.seleDate = data.aftersale.seleDate
                this.dataForm.forId = data.aftersale.forId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/patient/aftersale/${!this.dataForm.afterId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'afterId': this.dataForm.afterId || undefined,
                'orderNum': this.dataForm.orderNum,
                'freight': this.dataForm.freight,
                'saleLeave': this.dataForm.saleLeave,
                'seleImg': this.dataForm.seleImg,
                'seleDate': this.dataForm.seleDate,
                'forId': this.dataForm.forId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
